﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTimKiemHangHoa : Form
    {
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        public event FormCloseTimKiemHangHoa formclosetimkiemhanghoa = null;
        public string NoiLuu = null ;
        public string ChuoiTimKiem = null;
        public string MaKho = null;

        public FrmTimKiemHangHoa()
        {
            InitializeComponent();
        }

        public FrmTimKiemHangHoa(string sTimKiem, string sMaKho, string sNoiLuu)
        {
            InitializeComponent();
            this.ChuoiTimKiem = sTimKiem;
            this.MaKho = sMaKho;
            this.NoiLuu = sNoiLuu;
        }

        private void FrmTimKiemHangHoa_Load(object sender, EventArgs e)
        {
            ResultObj resultobj = new ResultObj();
            dgvTimKiemHangHoa.AutoGenerateColumns = false;
            
            switch (MaKho)
            {
                case "152":
                    resultobj = hanghoaMgt.GetAllHangHoa();
                    List<HangHoa> lstVatLieu = (from c in (List<HangHoa>)resultobj.Result
                                                where c.Ten.ToLower().Contains(ChuoiTimKiem.ToLower())
                                                select c).ToList();

                    if (lstVatLieu.Count > 0)
                        dgvTimKiemHangHoa.DataSource = lstVatLieu;
                    else
                    {
                        MessageBox.Show("Không có dữ liệu phù hợp");
                        this.Close();
                    }
                    break;
                default:
                    resultobj = hanghoaMgt.GetAllHangHoa();
                    List<HangHoa> lstHangHoa = (from c in (List<HangHoa>)resultobj.Result
                                                where c.Ten.Contains(ChuoiTimKiem.ToLower()) && c.NoiLuuTru == NoiLuu
                                                select c).ToList();

                    if (lstHangHoa.Count > 0)
                        dgvTimKiemHangHoa.DataSource = lstHangHoa;
                    else
                    {
                        MessageBox.Show("Không có dữ liệu phù hợp");
                        this.Close();
                    }
                    break;
            }
         }

        private void dgvTimKiemHangHoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (formclosetimkiemhanghoa != null)
                {
                    formclosetimkiemhanghoa(dgvTimKiemHangHoa.CurrentRow.Cells[2].Value.ToString(), dgvTimKiemHangHoa.CurrentRow.Cells[0].Value.ToString(), false);
                }
                this.Close();
            }
        }
    }
}
